package com.nkzy.Controller;

import com.nkzy.pojo.Result;
import com.nkzy.pojo.User;
import com.nkzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping
    public Result<Integer> insertUser(@RequestBody User user) {
        int result = userService.insertUser(user);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    public Result<Integer> deleteUserById(@PathVariable Long id) {
        int result = userService.deleteUserById(id);
        return Result.success(result);
    }

    @PutMapping
    public Result<Integer> updateUser(@RequestBody User user) {
        int result = userService.updateUser(user);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    public Result<User> selectUserById(@PathVariable Long id) {
        User user = userService.selectUserById(id);
        return Result.success(user);
    }

    @GetMapping("/list")
    public Result<List<User>> selectAllUsers() {
        List<User> users = userService.selectAllUsers();
        return Result.success(users);
    }

    @GetMapping("/ids")
    public Result<List<User>> selectUsersByIds(@RequestParam List<Long> ids) {
        List<User> users = userService.selectUsersByIds(ids);
        return Result.success(users);
    }
}